import "package:ct_pa_majordomo/pages/mine/withdraw/api.dart";
import "package:flutter/material.dart";
import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";

class WithdrawDetailPage extends StatefulWidget {
  final int withdrawId;
  const WithdrawDetailPage({super.key, required this.withdrawId});

  @override
  State<WithdrawDetailPage> createState() => _WithdrawDetailPageState();
}

class _WithdrawDetailPageState extends State<WithdrawDetailPage> {
  Map<String, dynamic> _withdrawDetail = {};
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _loadWithdrawDetail();
  }

  // 加载提现详情
  void _loadWithdrawDetail() {
    setState(() {
      _isLoading = true;
    });

    withdrawApi.withdrawDetail(widget.withdrawId).then((value) {
      if (mounted) {
        setState(() {
          _isLoading = false;
          _withdrawDetail = value["data"] ?? {};
        });
      }
    }).catchError((error) {
      if (mounted) {
        setState(() {
          _isLoading = false;
        });
      }
    });
  }

  // 获取状态文本
  String _getStatusText(Map<String, dynamic> withdraw) {
    // 有三个字段代表状态，status，auditStatus，paymentStatus， 0代表未处理，1代表处理完成，2代表处理失败
    if (withdraw["paymentStatus"] == "1") {
      return "已完成";
    } else if (withdraw["paymentStatus"] == "2") {
      return "提现失败";
    } else {
      if (withdraw["auditStatus"] == "1") {
        return "审核通过";
      } else if (withdraw["auditStatus"] == "2") {
        return "审核拒绝";
      } else {
        return "审核中";
      }
    }
  }

  // 获取状态颜色
  Color _getStatusColor(String status) {
    switch (status) {
      case "审核中":
        return ThemeVars.colorWarning;
      case "审核通过":
        return ThemeVars.colorSuccess;
      case "审核拒绝":
        return ThemeVars.colorError;
      case "提现失败":
        return ThemeVars.colorError;
      default:
        return ThemeVars.colorTextSec;
    }
  }


  // 查看账单明细
  void _viewBillList() {
    Stacker.pushNamed(Keys.WITHDRAW_BILL_LIST, arguments: {"withdrawId": widget.withdrawId});
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "提现详情",
      actions: [
        TextButton(
          onPressed: _viewBillList,
          child: const Text("关联账单", style: TextStyle(color: Colors.white),),
        ),
      ],
      child: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : SingleChildScrollView(
              padding: const EdgeInsets.all(ThemeVars.paddingPage),
              child: Column(
                children: [
                  // 提现状态卡片
                  Container(
                    width: double.infinity,
                    padding: const EdgeInsets.all(20),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.1),
                          blurRadius: 4,
                          offset: const Offset(0, 2),
                        ),
                      ],
                    ),
                    child: Column(
                      children: [
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                          decoration: BoxDecoration(
                            color: _getStatusColor(_getStatusText(_withdrawDetail)).withOpacity(0.1),
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Text(
                            _getStatusText(_withdrawDetail),
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                              color: _getStatusColor(_getStatusText(_withdrawDetail)),
                            ),
                          ),
                        ),
                        const SizedBox(height: 16),
                        Text(
                          "¥${Utils.formatCurrency(_withdrawDetail["amount"] ?? 0)}",
                          style: const TextStyle(
                            fontSize: 32,
                            fontWeight: FontWeight.bold,
                            color: ThemeVars.colorPrimary,
                          ),
                        ),
                        const SizedBox(height: 8),
                        Text(
                          "提现金额",
                          style: const TextStyle(
                            fontSize: 14,
                            color: ThemeVars.colorTextSec,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 16),

                  // 提现详情信息
                  Container(
                    width: double.infinity,
                    padding: const EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                      border: Border.all(
                        color: ThemeVars.colorBorder,
                        width: 0.5,
                      ),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          "提现信息",
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        const SizedBox(height: 16),
                        _buildInfoRow("提现单号", "#${_withdrawDetail["withdrawId"] ?? ""}"),
                        _buildInfoRow("申请时间", _withdrawDetail["applyTime"] ?? ""),
                        _buildInfoRow("审核时间", _withdrawDetail["auditTime"] ?? ""),
                        _buildInfoRow("完成时间", _withdrawDetail["paymentTime"] ?? ""),
                        _buildInfoRow("备注", _withdrawDetail["remark"] ?? "")
                      ],
                    ),
                  ),
                ],
              ),
            ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 80,
            child: Text(
              label,
              style: const TextStyle(
                fontSize: 14,
                color: ThemeVars.colorTextSec,
              ),
            ),
          ),
          Expanded(
            child: Text(
              value == null || value.isEmpty ? '--' : value,
              style: const TextStyle(
                fontSize: 14,
                color: ThemeVars.colorText,
              ),
            ),
          ),
        ],
      ),
    );
  }
} 